class RoomsController < ApplicationController
  
  before_filter :login_required
  
  # GET /rooms
  # GET /rooms.xml
  def index
    @title = "Index Rooms"
    @rooms = Room.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @rooms }
    end
  end

  # GET /rooms/1
  # GET /rooms/1.xml
  def show
    @title = "Show Rooms"
    @room = Room.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @room }
    end
  end

  # GET /rooms/new
  # GET /rooms/new.xml
  def new
    @title = "New Rooms"
    @room = Room.new
    
    @companies = Company.find(:all)

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @room }
    end
  end

  # GET /rooms/1/edit
  def edit
    @title = "Edit Rooms"
    @room = Room.find(params[:id])
    @companies = Company.find(:all)
  end

  # POST /rooms
  # POST /rooms.xml
  def create
    @title = "Create Rooms"
    @room = Room.new(params[:room])
    if request.xhr?
        if @room.save
          @companies = Company.all
          @computers = []
          for computer in Computer.all
            if (computer.room_id == nil) 
              @computers << computer
            end
          end
          render :update do |page|
            page.replace_html 'tabs-1', :partial => 'supervisions/tree', :locals => {:companies => @companies, :computers => @computers}
          end
        else
          render :nothing => true
        end
      else    
        respond_to do |format|
          if @room.save
            flash[:notice] = 'Room was successfully created.'
            format.html { redirect_to(@room) }
            format.xml  { render :xml => @room, :status => :created, :location => @room }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @room.errors, :status => :unprocessable_entity }
          end
        end
      end
  end

  # PUT /rooms/1
  # PUT /rooms/1.xml
  def update
	@title = "Update Rooms"
    @room = Room.find(params[:id])

    respond_to do |format|
      if @room.update_attributes(params[:room])
        flash[:notice] = 'Room was successfully updated.'
        format.html { redirect_to(@room) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @room.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /rooms/1
  # DELETE /rooms/1.xml
  def destroy
    @title = "Destroy Room"
    @room = Room.find(params[:id])
    @room.destroy

    respond_to do |format|
      format.html { redirect_to(rooms_url) }
      format.xml  { head :ok }
    end
  end
  
      def add_network
    
    network = Network.find(params[:network_id])
    room = Room.find(params[:room_id])
    room.networks << network
    
    render :update do |page|
      page.remove "network_#{params[:network_id]}"
      page.insert_html :bottom , 'container_network', :partial => 'rooms/room_network', :locals => {:network => network}
    end
 
  end

  def delete_network
    
    network = Network.find(params[:network_id])
    room = Room.find(params[:room_id])
    room.networks.delete(network)
      
    render :update do |page|
      page.remove "network_#{params[:network_id]}"
      page.insert_html :bottom , 'trash_network', :partial => 'rooms/room_network', :locals => {:network => network}
    end
    
  end

end
